package cc.openkit.admin.controller.project;

import cc.openkit.admin.controller.admin.AdminController;
import cc.openkit.admin.model.Admin;
import cc.openkit.admin.util.FileUpdateCopyUtil;
import cc.openkit.admin.util.StaticFinalVar;
import cc.openkit.admin.util.ZipUtils;
import cc.openkit.admin.vo.LimitVo;
import cc.openkit.admin.vo.ProjectVo;
import cc.openkit.common.KitUtil;
import com.alibaba.fastjson.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代码生成工具
 *
 * @author bigben
 * @date 2018.2.5
 */
@Controller
@Scope("prototype")
@RequestMapping("/peoject")
public class ProjectController {
    private Logger log = Logger.getLogger(AdminController.class);

    /**
     * 登录验证方法
     * @param request
     * @param session
     * @return
     */
    @RequestMapping(value = "/index", method = RequestMethod.POST)
    @ResponseBody
    public Object index(HttpServletRequest request, HttpSession session){
        log.info("开始生成代码");
        // 获取前台的数据
        String projectType = request.getParameter("project_type");
//        String[] support = request.getParameterValues("support");
        String projectName = request.getParameter("project_name");
        String sign = request.getParameter("sign");
        String author = request.getParameter("author");
        String web = request.getParameter("web");
        String describe = request.getParameter("describe");

        // 封装对象
        ProjectVo projectVo = new ProjectVo(projectType, "111", projectName, sign, author, web, describe);

        // 准备复制
        FileUpdateCopyUtil fileUpdateCopyUtil = new FileUpdateCopyUtil();
        fileUpdateCopyUtil.fileCopy(projectVo);

        // 开始压缩
        ZipUtils zipUtils = new ZipUtils();
        String downloadUrl = "";
        try {
            downloadUrl = zipUtils.zip(projectVo);
        } catch (Exception e) {
            e.printStackTrace();
        }

        String url = StaticFinalVar.WEB_URL+"/templates/style/muban/zip/"+sign+"_openkit_V1.0.zip";

        return JSONObject.toJSON(KitUtil.returnMap("200", url));
    }

}
